CKEDITOR.plugins.add('pdfPrinter',{
  
  init: function(editor){
    var pluginName = 'pdfPrinter';
    
    CKEDITOR.dialog.add(pluginName, this.path + 'dialogs/pdfPrinter.js');
    editor.addCommand(pluginName, {
		modes : { wysiwyg:1, source:1 },
		canUndo : false,
		exec : function( editor )
		{ 
      var sHTML,
				config = editor.config,
				baseTag = config.baseHref ? '<base href="' + config.baseHref + '"/>' : '',
				isCustomDomain = CKEDITOR.env.isCustomDomain();

			if ( config.fullPage )
			{
				sHTML = editor.getData()
						.replace( /<head>/, '$&' + baseTag )
						.replace( /[^>]*(?=<\/title>)/, editor.lang.preview );
			}
			else
			{
				var bodyHtml = '<body ',
						body = editor.document && editor.document.getBody();

				if ( body )
				{
					if ( body.getAttribute( 'id' ) )
						bodyHtml += 'id="' + body.getAttribute( 'id' ) + '" ';
					if ( body.getAttribute( 'class' ) )
						bodyHtml += 'class="' + body.getAttribute( 'class' ) + '" ';
				}

				bodyHtml += '>';

				sHTML =
					editor.config.docType +
					'<html dir="' + editor.config.contentsLangDirection + '">' +
					'<head>' +
					baseTag +
					'<title>' + editor.lang.preview + '</title>' +
					CKEDITOR.tools.buildStyleHtml( editor.config.contentsCss ) +
					'</head>' + bodyHtml +
					editor.getData() +
					'</body></html>';
			}

			var iWidth	= 640,	// 800 * 0.8,
				iHeight	= 420,	// 600 * 0.7,
				iLeft	= 80;	// (800 - 0.8 * 800) /2 = 800 * 0.1.
			try
			{
				var screen = window.screen;
				iWidth = Math.round( screen.width * 0.8 );
				iHeight = Math.round( screen.height * 0.7 );
				iLeft = Math.round( screen.width * 0.1 );
			}
			catch ( e ){}
      
      if(CKEDITOR.pdfApplet){ 
		    CKEDITOR.pdfApplet.printPDF(sHTML);
      }else{
        alert('An error occured.\nYou will not be able to print a PDF, because of the following reasons:\n1. You do not have Java 1.5 or above.\n2. You have not accepted the certificate for the Java applet.\n3. Your browser does not support Java, or is turned off.');
      }
			
		}});
    
    editor.ui.addButton('PDFPrinter',
        {
            label: 'Print PDF',
            command: pluginName,
            icon: CKEDITOR.plugins.getPath('pdfPrinter') + 'images/pdfPrinter.png'
        });
  }
});
